Using resload_Protect#?

Theory

There are various situations in which it maybe very useful to be informed when the installed program makes accesses to certain specific memory locations. With the resload_Protect#? functions it is possible to protect certain memory locations from reading and/or writing by the processor. Protecting means that every access to such a protected area if performed will create an Access Fault exception which will result in an appropriate requester by WHDLoad. If you declare a memory area as protected using a resload_Protect#? function WHDLoad will modify the affected page descriptors in the MMU translation tree. Now on every access to the protected page the CPU will create an Access Fault exception. The exception handler inside WHDLoad will verify the reason for the exception. If the reason was an access to a protected page but the access does not match the protected area the access will be emulated, and normal program execution continues. Otherwise WHDLoad will quit with an appropriate requester. If the access was an access to the instruction stream (i.e. the cpu attempts to load code) it will always be emulated, or with other words the resload_Protect#? functions only affects reading and writing of data. The fact that every access to a protected page (pagesize is currently $1000) even if the protected area has only a length of 1 byte will create an access fault results in a strong slow down of the execution speed of the program. Especially if parts of the code are located on the same page. If the program depends on execution speed, differences in the execution are possible. So it maybe possible that some programs will not work with the protected feature.

Example: checksums over code

If you install a game using WHDLoad you have to patch the original loader routines in the game in a way that they will use WHDLoad to load the game data. Some games are performing checksums over certain code areas to detect if the original code has been modified. These detection routines may sometimes be hard to find. But using the resload_Protect#? functions in WHDLoad nothing is easier than this. All you have to do is to protect the bytes you changed in the games code from reading. Now every routine which tries to make a checksum and read your patched code will cause an access fault. And you will know where the routine is located.

Limitations

You must not protect the memory page where the SSP points to. If you do so, and an Exception occurs, it will result in a Double Bus Fault because the CPU will be unable to write the exception stackframe. After a Double Bus Fault only a reset can be performed to continue execution.